<template>
  <div class="app-container">
    <el-descriptions
      title="问答详情"
      :column="3"
      style="padding: 15px; border: 1px solid #ebeef5"
    >
      <el-descriptions-item label="唯一标识">{{
        detail.uqid
      }}</el-descriptions-item>
      <el-descriptions-item label="提问地址">{{
        detail.address
      }}</el-descriptions-item>
      <el-descriptions-item label="审核状态">{{
        detail.status | statusFilter
      }}</el-descriptions-item>
      <el-descriptions-item label="标签">
        <el-tag style="margin-right:10px;" size="small" v-for="(tag, index) in detail.tags" :key="index">{{
          tag
        }}</el-tag>
      </el-descriptions-item>
      <el-descriptions-item label="提问者">{{
        detail.uuid
      }}</el-descriptions-item>
      <el-descriptions-item label="提问时间">{{
        detail.ctime
      }}</el-descriptions-item>
      <el-descriptions-item label="点赞量">{{
        detail.supportCount
      }}</el-descriptions-item>
      <el-descriptions-item label="浏览量">{{
        detail.viewCount
      }}</el-descriptions-item>
      <el-descriptions-item label="回答量">{{
        detail.answerCount
      }}</el-descriptions-item>
      <el-descriptions-item label="提问标题" :span="3"
        ><b>{{ detail.title }}</b></el-descriptions-item
      >
      <el-descriptions-item label="提问描述" :span="3"
        ><b>{{ detail.content }}</b></el-descriptions-item
      >
    </el-descriptions>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>回答列表</span>
    </el-card>
    <div class="table-container">
      <el-tabs v-model="statusTab" type="card" @tab-click="handleClickTabs">
        <el-tab-pane
          :label="'全部(' + countMap.allCount + ')'"
          name="-1"
        ></el-tab-pane>
        <el-tab-pane
          :label="'待审核(先发后审)(' + countMap.issueReviewCount + ')'"
          name="10"
        ></el-tab-pane>
        <el-tab-pane
          :label="'待审核(先审后发)(' + countMap.reviewIssueCount + ')'"
          name="20"
        >
        </el-tab-pane>
        <el-tab-pane
          :label="'已下架(' + countMap.offTheShelfCount + ')'"
          name="40"
        ></el-tab-pane>
        <el-tab-pane
          :label="'审核通过(' + countMap.approvedCount + ')'"
          name="30"
        ></el-tab-pane>

        <el-tab-pane
          :label="'审核未通过(' + countMap.notPassedCount + ')'"
          name="0"
        ></el-tab-pane>
      </el-tabs>
      <el-table
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column
          type="selection"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column label="编号" width="70" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="回答内容" align="center">
          <template slot-scope="scope">
            <b v-html="scope.row.content"></b>
          </template>
        </el-table-column>
        <el-table-column label="点赞量" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.likedCount }}</template>
        </el-table-column>
        <el-table-column label="点踩量" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.hateCount }}</template>
        </el-table-column>
        <el-table-column label="回答时间" width="160" align="center">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column label="回答者" width="100" align="center">
          <template slot-scope="scope"
            ><a>{{ scope.row.userNickname }}</a>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" width="120" align="center">
          <template slot-scope="scope">
            <p
              :class="
                scope.row.status === 30 ? 'success-status' : 'error-status'
              "
            >
              {{ scope.row.status | statusFilter }}
            </p>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <p>
              <el-button
                size="mini"
                @click="handleVerify(scope.$index, scope.row)"
                >审核
              </el-button>
              <el-button
                size="mini"
                type="primary"
                @click="handleViewDetail(scope.$index, scope.row)"
                >查看详情
              </el-button>
            </p>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-select size="small" v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operates"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small"
      >
        确定
      </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[10, 15, 20, 50]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
      <div style="height: 20px"></div>
    </div>
    <el-dialog title="审核" :visible.sync="verifyDialogVisible" width="30%">
      <el-form :model="verifyForm" label-width="80px">
        <el-form-item label="审核结果">
          <el-radio-group v-model="verifyForm.verifyStatus" size="medium">
            <el-radio :label="40">下架</el-radio>
            <el-radio :label="30">通过</el-radio>
            <el-radio :label="0">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            placeholder="请输入内容"
            v-model="verifyForm.detail"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleVerifySubmit" size="small"
          >确 定</el-button
        >
        <el-button @click="verifyDialogVisible = false" size="small"
          >取 消</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryItem } from "@/api/question";
import { fetchList, getCounts,updateStatus } from "@/api/comment.js";
import { getStatusDes } from "@/utils/validate.js";
const defaultDetail = {
  uqid: null,
  uuid: null,
  userAvatar: null,
  userNickname: null,
  address: null,
  pid: null,
  partitionName: null,
  title: null,
  supportCount: null,
  answerCount: null,
  viewCount: null,
  ctime: null,
  status: null,
  content: null,
  tags: [],
};

const defaultListQuery = {
  mediaId: null,
  // 问答
  mediaType: 5,
  status: null,
  pageNum: 1,
  pageSize: 10,
};
export default {
  data() {
    return {
      detail: Object.assign({}, defaultDetail),
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      // 审核回答
      verifyDialogVisible: false,
      verifyForm: {
        ids: [],
        verifyStatus: 30,
        detail: "",
      },
      statusTab: "-1",
      countMap: {
        allCount: 0,
        offTheShelfCount: 0,
        approvedCount: 0,
        reviewIssueCount: 0,
        issueReviewCount: 0,
        notPassedCount: 0,
      },
      multipleSelection: [],
      listLoading: false,
      operates: [
        {
          label: "批量审核",
          value: "verify",
        },
      ],
      operateType: "verify",
    };
  },
  created() {
    this.getQuestionDetail();
    this.listQuery.mediaId = this.$route.query.uqid;
    this.getList();
  },
  filters: {
    statusFilter(value) {
      return getStatusDes(value);
    },
  },
  methods: {
    handleClickTabs(tab, event) {
      var status = Number(this.statusTab);
      if (status !== -1) {
        this.listQuery.status = status;
      } else {
        this.listQuery.status = null;
      }
      this.getList();
      this.getCountMap();
    },
    getQuestionDetail() {
      queryItem(this.$route.query.uqid).then((response) => {
        console.log(response.data);
        this.detail = response.data;
        // this.list = response.data.comments.list;
        // this.total = response.data.comments.total;
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleVerify(id) {
      this.verifyDialogVisible = true;
      this.verifyForm.ids = [id];
    },
    handleVerifySubmit() {
      let params = new URLSearchParams();
      params.append("ids", this.verifyForm.ids);
      params.append("verifyStatus", this.verifyForm.verifyStatus);
      params.append("detail", this.verifyForm.detail);
      updateStatus(params).then((res) => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000,
        });
        this.verifyDialogVisible = false;
      });
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
    getCountMap() {
      getCounts(this.listQuery).then((res) => {
        this.countMap = res.data;
      });
    },
    handleViewDetail(index, row) {
      this.$router.push({
        path: "/cms/commentDetail",
        query: { id: row.id },
      });
    },
    handleBatchOperate() {
      if (this.operateType == null) {
        this.$message({
          message: "请选择操作类型",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      if (this.multipleSelection == null || this.multipleSelection.length < 1) {
        this.$message({
          message: "请选择要操作的回答",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      let ids = [];
      for (let i = 0; i < this.multipleSelection.length; i++) {
        ids.push(this.multipleSelection[i].id);
      }
      this.verifyDialogVisible = true;
      this.verifyForm.ids = ids;
    },
  },
};
</script>

<style lang="scss" scoped>
.item {
  margin-bottom: 18px;
}
.success-status {
  background-color: #67c23a;
  color: white;
}
.error-status {
  background-color: #f56c6c;
  color: white;
}
</style>